<template>
  <div class="dashboard-container">
    <div class="app-container" />
    <el-card>
      <!-- 按钮 -->
      <el-row type="flex" justify="end">
        <el-button type="primary" @click="addpermist(1,'0')">添加权限</el-button>
      </el-row>

      <el-table :data="permList" row-key="id" default-expand-all>
        <el-table-column label="权限名称" prop="name" />
        <el-table-column label="权限标记" prop="code" />
        <el-table-column label="权限描述" prop="description" />
        <el-table-column label="操作按钮">
          <template v-slot="scoped">
            <el-button v-if="scoped.row.type===1" type="text" @click="addpermist(2,scoped.row.id)">
              添加权限
            </el-button>
            <el-button type="text" @click="updatePerm(scoped.row.id)">
              编辑权限
            </el-button>
            <el-button type="text" @click="delPerm(scoped.row.id)">
              删除权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 放置一个弹层 用来编辑新增节点 -->
    <el-dialog :visible="showDialog" :title="formData.id? '编辑权限':'添加权限'" @close="closeBtn">
      <!-- 表单 -->
      <el-form ref="addpermist" :model="formData" reflabel-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="closeBtn">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { getPermList, addPermList, getPermDetail, updatePerm, delPerm } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      permList: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        descrition: '',
        pid: '0',
        enVisible: '1', // enVisible 是否激活权限,字符串1为激活, 字符串0 为禁用
        type: 1 // type 是指权限层级,第一层为1,第二层为2
      }
    }
  },
  created() {
    this.getPermist()
  },
  methods: {
    async getPermist() {
      this.permList = listToTreeData(await getPermList(), '0')
      console.log(48, this.permList)
    },
    async addpermist(type, pid) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    async updatePerm(id) {
      this.formData = await getPermDetail(id)
      this.showDialog = true
    },
    async delPerm(id) {
      // 询问客户
      await this.$confirm('是否删除')
      // 发请求
      await delPerm(id)
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.showDialog = false
      // 清空数据
      this.getPermist()
    },
    async btnOk() {
      // 校验表单
      await this.$refs.addpermist.validate()
      // 根据id 判断调用编辑还是新增
      if (this.formData.id) {
        // 编辑
        await updatePerm(this.formData)
      } else {
        // 新增
        await addPermList(this.formData)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.showDialog = false
      // 刷新页面
      this.getPermist()
    },
    async closeBtn() {
      // 清空数据
      this.formData = {
        name: '',
        code: '',
        descrition: '',
        pid: '',
        enVisible: '1', // enVisible 是否激活权限,字符串1为激活, 字符串0 为禁用
        type: 1 // type 是指权限层级,第一层为1,第二层为2
      }
      // 清理报错
      this.$refs.addpermist.resetFields()
      // 关闭弹窗
      this.showDialog = false
    }

  }
}
</script>

<style>

</style>
